package com.swotxu.mall.order.controller;

import com.swotxu.mall.order.entity.OrderEntity;
import com.swotxu.mall.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.UUID;

/**
 * @Date: 2022/4/29 23:19
 * @Author: swotXu
 */
@Slf4j
@Controller
@RequestMapping("test")
public class TestController {
    private static final String RABBITMQ_EXCHANGE = "hello-java-exchange";
    private static final String RABBITMQ_QUEUE = "hello-java-queue";
    private static final String RABBITMQ_ROUTING_KEY = "hello.java";

    @Autowired
    RabbitTemplate rabbitTemplate;

    @ResponseBody
    @RequestMapping("/sendMsg")
    public String sendMsg(@RequestParam("num") Integer num) {
        for (int i = 0; i < num; i++) {
            if (i % 2 == 0) {
                OrderReturnReasonEntity reasonEntity = new OrderReturnReasonEntity();
                reasonEntity.setId(1L);
                reasonEntity.setCreateTime(new Date());
                reasonEntity.setName("哈哈-" + i);
                rabbitTemplate.convertAndSend(RABBITMQ_EXCHANGE, RABBITMQ_ROUTING_KEY, reasonEntity, new CorrelationData(UUID.randomUUID().toString()));
            } else {
                OrderEntity entity = new OrderEntity();
                entity.setOrderSn(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend(RABBITMQ_EXCHANGE, RABBITMQ_ROUTING_KEY, entity, new CorrelationData(UUID.randomUUID().toString()));
            }
            log.info("[{}]消息发送完成!", i);
        }
        return "ok:" + num;
    }

    @RequestMapping("/{page}.html")
    public String htmlPage(@PathVariable("page") String page) {
        return page;
    }
}
